package com.itheima.netmarket.impl.view;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itheima.netmarket.GlobalConstants;
import com.itheima.netmarket.impl.view.manager.UIManagerImpl;
import comitheima.netmarket.R;

/**
 * 支付方式选择view
 * 
 * @author yangxin
 */
public class PayTypeView extends MainView {

	/**
	 * 头部返回按钮
	 */
	private TextView headBackText;
	/**
	 * 到付现金
	 */
	private RelativeLayout payMoneyRel;
	/**
	 * 到付pos机
	 */
	private RelativeLayout payPosRel;
	/**
	 * 支付宝
	 */
	private RelativeLayout payAlipayRel;
	/**
	 * 到付现金选择
	 */
	private ImageView payMoneyImg;
	/**
	 * 到付pos机选择
	 */
	private ImageView payPosImg;
	/**
	 * 支付宝选择
	 */
	private ImageView payAlipayImg;

	private TextView payMoneyText;
	private TextView payPosText;
	private TextView payAlipayText;

	public PayTypeView(Context context, Bundle bundle) {
		super(context, bundle);
	}

	public PayTypeView(Context context) {
		super(context);
	}

	@Override
	public View getmView() {
		View view = View.inflate(context, R.layout.paytype_activity, null);
		init(view);
		return view;
	}

	/**
	 * 界面初始化
	 * 
	 * @param view
	 */
	private void init(View view) {
		headBackText = (TextView) view.findViewById(R.id.head_back_text);
		payMoneyRel = (RelativeLayout) view.findViewById(R.id.pay_money_rel);
		payPosRel = (RelativeLayout) view.findViewById(R.id.pay_pos_rel);
		payAlipayRel = (RelativeLayout) view.findViewById(R.id.pay_alipay_rel);
		payMoneyImg = (ImageView) view.findViewById(R.id.pay_money_img);
		payPosImg = (ImageView) view.findViewById(R.id.pay_pos_img);
		payAlipayImg = (ImageView) view.findViewById(R.id.pay_alipay_img);
		payMoneyText = (TextView) view.findViewById(R.id.pay_money_text);
		payPosText = (TextView) view.findViewById(R.id.pay_pos_text);
		payAlipayText = (TextView) view.findViewById(R.id.pay_alipay_text);

		setOnclick();
		clearChoose();
	}

	/**
	 * 设置点击事件
	 */
	private void setOnclick() {
		headBackText.setOnClickListener(this);
		payMoneyRel.setOnClickListener(this);
		payPosRel.setOnClickListener(this);
		payAlipayRel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.head_back_text:
			// 头部返回按钮
			UIManagerImpl.getInstance().pressBack();
			bundle = new Bundle();
			bundle.putString("payTypeContent", GlobalConstants.payTypeContent);
			break;
		case R.id.pay_money_rel:
			// 到付现金
			if (payMoneyImg.getVisibility() == View.GONE) {
				clearChoose();
				payMoneyImg.setVisibility(View.VISIBLE);
				GlobalConstants.payTypeContent = payMoneyText.getText().toString();
			} else
				payMoneyImg.setVisibility(View.GONE);
			break;
		case R.id.pay_pos_rel:
			// 到付pos机
			if (payPosImg.getVisibility() == View.VISIBLE) {
				payPosImg.setVisibility(View.GONE);
			} else {
				clearChoose();
				GlobalConstants.payTypeContent = payPosText.getText().toString();
				payPosImg.setVisibility(View.VISIBLE);
			}
			break;
		case R.id.pay_alipay_rel:
			// 支付宝
			if (payAlipayImg.getVisibility() == View.VISIBLE)
				payAlipayImg.setVisibility(View.GONE);
			else {
				clearChoose();
				payAlipayImg.setVisibility(View.VISIBLE);
				GlobalConstants.payTypeContent = payAlipayText.getText().toString();
			}
			break;
		}
	}

	/**
	 * 清除选择
	 */
	private void clearChoose() {
		payMoneyImg.setVisibility(View.GONE);
		payPosImg.setVisibility(View.GONE);
		payAlipayImg.setVisibility(View.GONE);
	}
}
